package edu.zzuli.assistant.until;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import edu.zzuli.assistant.api.util.HibernateProxyTypeAdapter;
import edu.zzuli.assistant.api.util.NormalStrategy;

public class StrUtil {

	/**
	 * @param args
	 */
	
	/**
	 * 日期+随机数   16 位
	 * @return
	 */
	public static String  getTimeStr(){
	    int max=1000;
	    int min=1;
	    Random random = new Random();
	    int randomInt = random.nextInt(max)%(max-min+1) + min;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");  
        String newFileName = df.format(new Date())  + randomInt;
		return newFileName;  
	} 
	
	public static Date  getFailDate(){
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, 2);
		return calendar.getTime();
		
	} 
	public static String  getKey(){
		String key = MD5Utiil.encrypt16(getTimeStr()+"ZZULI");
		return  key;
		
	} 
	public static void sendMsg(String content) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter print=null;
		try {
			print=response.getWriter();
			print.print(content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (response!=null) {
			    print.flush();
				print.close();
			}
		}
	}
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param clazz	                         特殊的对象属性
	 * @param clazzFields     特殊的对象属性 需要转成json的属性
	 * @param object		     要转的内容
	 */
	public static void sendJson(Class targetClass,String[] fields,Class clazz,String[]  clazzFields,Object object) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print=null;
		try {
			print=response.getWriter();
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setClazz(clazz);
			ts.setClazzFields(clazzFields);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			String jsonString  = gson.toJson(object);
			print.print(jsonString);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (response!=null) {
			    print.flush();
				print.close();
			}
		}
	}
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param clazz	                         特殊的对象属性
	 * @param clazzFields     特殊的对象属性 需要转成json的属性
	 * @param object		     要转的内容
	 */
	public static String paseJson(Class targetClass,String[] fields,Class clazz,String[]  clazzFields,Object object) {
		String jsonString = null;
		try {
			
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setClazz(clazz);
			ts.setClazzFields(clazzFields);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			jsonString  = gson.toJson(object);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
	
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param object		     要转的内容
	 */
	public static void sendJson(Class targetClass,String[] fields,Object object) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print=null;
		try {
			print=response.getWriter();
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			String jsonString  = gson.toJson(object);
			print.print(jsonString);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (response!=null) {
				print.flush();
				print.close();
			}
		}
	}
	public static  void sendJson(Object object) {
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Gson gson = new GsonBuilder()  
			.setDateFormat("yyyy-MM-dd HH:mm:ss")  
			.create();
			String jsonString  = gson.toJson(object); 
			out.println(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
}
